package com.smartmedi.ai.controller;

import com.smartmedi.ai.common.BaseResponse;
import com.smartmedi.ai.common.ResultUtils;
import io.swagger.v3.oas.annotations.Operation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * 健康检查控制器
 */
@RestController
@RequestMapping("/health")
@Slf4j
public class HealthyController {

    @GetMapping
    @Operation(summary = "健康检查")
    public String check() {
        return "ok";
    }

    @GetMapping("/api")
    @Operation(summary = "API健康检查")
    public BaseResponse<Map<String, Object>> checkApi() {
        Map<String, Object> data = new HashMap<>();
        data.put("status", "up");
        data.put("message", "API服务运行正常");
        data.put("timestamp", System.currentTimeMillis());
        return ResultUtils.success(data);
    }
    
    @GetMapping("/timeout-test")
    @Operation(summary = "超时测试接口", description = "测试超时异常处理，可控制是否真实超时")
    public BaseResponse<Object> testTimeout(boolean shouldTimeout) {
        log.info("触发超时测试, shouldTimeout={}", shouldTimeout);
        
        if (shouldTimeout) {
            try {
                // 模拟处理延迟
                log.info("开始模拟超时...");
                TimeUnit.SECONDS.sleep(3);
                log.info("模拟超时完成");
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
                log.error("超时测试被中断", e);
            }
        }
        
        Map<String, Object> data = new HashMap<>();
        data.put("handled", true);
        data.put("message", shouldTimeout ? "已处理超时请求" : "正常请求处理");
        data.put("timestamp", System.currentTimeMillis());
        
        return ResultUtils.success(data);
    }
}
